<?PHP

class SiteDiscountBO extends BusinessObject{
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"            =>	"sites_discount_id",
			"title"         =>	"sites_discount_title",
			"code"          =>	"sites_discount_code",
			"type"			=>	"sites_discount_type",
			"amount"        =>	"sites_discount_amount",
			"startdate"     =>	"sites_discount_startdate",
			"finishdate"    =>	"sites_discount_finishdate",
			"active"        =>	"sites_discount_active",
			"descr"			=>	"sites_discount_descr",
			"inclaff"		=>	"sites_discount_inclaff",
			"allitems"		=>	"sites_discount_allitems",
			"inclagents"	=>	"sites_discount_incagents",
			"usedow"		=>	"site_discount_usedow",
			"use_mon"		=>	"site_discount_Mon",
			"use_tue"		=>	"site_discount_Tue",
			"use_wed"		=>	"site_discount_Wed",
			"use_thu"		=>	"site_discount_Thu",
			"use_fri"		=>	"site_discount_Fri",
			"use_sat"		=>	"site_discount_Sat",
			"use_sun"		=>	"site_discount_Sun",
            "affiliate"     =>  "site_discount_affiliate"
		);

		$dskItem = new SiteDiscountItemBO ();
		$this->relations = array("{$dskItem->tableName}" => "{$this->tableName}.{$this->fields['id']} = {$dskItem->tableName}.{$dskItem->fields['discount']}"
		);
    BusinessObject::setAttributes();
  }
  /* constuctor */
  function SiteDiscountBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("sites_discount");
    $this->setAttributes();
  }

  function calculate($price)
  {
        $amount = 0;        
		if ($this->type == 0) 
		{
			$amount = $price - (($price/100) * $this->amount);
		} else if($this->type == 1) 
		{
			$amount = $price - $this->amount;
		}else {
  			$amount = 0;  //free
		}
		return $amount;
  }

  function asString()
  {
        $result = "";
    	if ($this->type == 0)
    	{
    		$result =  $this->amount."%";
    	}else if($this->type == 1) 
    	{
    		$result = "$".$this->amount;
    	}else if($this->type == 2) 
    	{
    		$result =  "FREE";
    	}else if($this->type == 3)
    	{
    	    $result =  "BOGOF";
    	}
    	return $result;			 	        
  }
  
  function checkDiscount($start,$finish,$id="",$items) //return true if all ok with date ranges.
  {
      /* 
      //TODO: correct check including affiliates etc. 

      $sd = new SiteDiscountBO();
	  //$sd->active    = 1;
	  $where = "('".$start."' <= ".$sd->fields["finishdate"]." and '".$start."' >= ".$sd->fields["startdate"].") OR ".
    "('".$finish."' <= ".$sd->fields["finishdate"]." and '".$finish."' >= ".$sd->fields["startdate"].")";
	  if ($id != "")	$where = "(".$where.") and ".$sd->fields["id"]." <> ".$id;
	
	  $current_all = (count($items) == 0);
	
	  $sd->Load(0,0,array ($sd->fields["allitems"] => "1"),$where);
	  while($sd->Next())
	  {
          //if current special for ALL items and exist special in the same range - return FALSE
          if($current_all) return false; 
                
          if($sd->allitems=="1")
          {
              //if current special for some items BUT exist special for ALL items in the same range - return FALSE
              return false;    
          }else  
          {
            //this special for some items and current special for some items - need to check each item
            $special_items = new SiteDiscountItemBO();  
            $special_items->discount = $sd->id; 
            $special_items->id = $items;  //list of ID's
            if($special_items->getCount()>0) return false;  //exist active discount with the same date range
          }
     }
    //not exist specials in the same date range - return TRUE;
	*/
    return(true);
  }

  function returnPrice ($item, $price, $date, $isaff, $userid,$affID=0) //isaff yes or no
  { 
		$amount = $price;

		if(isset($_SESSION["AGENT_CODE"]) && $_SESSION["AGENT_CODE"]=="gc")
		{
		   return (round ($amount,2));  
		}
		
		$is_agent = false;
	  	if($userid!="")
	  	{
	  		$user = new UserBO ();
	  		$user->id = $userid;
	  		$user->Load();
	  		if($user->Next()){
	        	if($user->agn_agent==1) 
					$is_agent = true;
	      	}
	  	}
	  	
	  	if($isaff  && $affID>0) //if affiliate...need to check "ONLY FOR" this affiliate
	  	{
	  	        //now try to check specials for ALL items
        		$sd = new SiteDiscountBO();
        		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and ".$sd->fields["type"]."<3)";
        		$sd->allitems  = 1;
        		$sd->affiliate = $affID;
        		$sd->Load(0,1,"",$where);
        		if ($sd->Next()) //all items
        		{
    			    //if set "Apply only for selected days of week" but our current date not in this list so return original price 
    			    $current_day_of_week = strtolower(date("D", strtotime($date))); 
    			 	if ($sd->usedow == 1)     
    			 	{
    			 	    if($sd->{"use_".$current_day_of_week} == 1)
    			 	    {
                            $amount = $sd->calculate($price);
            			    return (round ($amount,2));    			 	        
    			 	    }
    				}else
    				{        				                       
                        $amount = $sd->calculate($price);
            			return (round ($amount,2));
    				}    
        		}

        		//now try to check specials for selected item        		
    			$sd             = new SiteDiscountBO();
    			$sd->affiliate  = $affID;
    			$dsk            = new SiteDiscountItemBO ();
    			$sd->leftJoin ($dsk);
    			$dsk->item      = $item;        			
    			$sd->Load(0,1,"",$where);
    			if ($sd->next())
    			{
    			    $current_day_of_week = strtolower(date("D", strtotime($date))); 
    			 	if ($sd->usedow == 1)     
    			 	{
    			 	    if($sd->{"use_".$current_day_of_week} == 1)
    			 	    {
        			 	    $amount = $sd->calculate($price);
            			    return (round ($amount,2));    			 	                			 	            			 	       
    			 	    }
    				}else
    				{        				                       
                        $amount = $sd->calculate($price);
            			return (round ($amount,2));
    				}    
    			}        		
	  	}//end of ONLY FOR AFFILIATE
	  	
        //====================================================================================================================================	  	
		$sd = new SiteDiscountBO();
		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and ".$sd->fields["type"]."<3)";
		$sd->allitems  = 1;
		$sd->Load(0,0,"",$where);
		if ($sd->next()) //all items
		{ 
			 if ($is_agent) 
			 {
				if ($sd->inclagents == 1) 
				{
					if ($sd->usedow == 1) 
					{
						$current_day_of_week = strtolower(date("D", strtotime($date)));
						if ($sd->{"use_".$current_day_of_week} != 1) //if selected day not TODAY...return agent's discount  
						{
								$amount = $price - $price / 100 * $user->agn_discountrate;
								return (round ($amount,2));
						}						
				    }else
				    {				    				    
				        $amount  = $sd->calculate($price);
					    $amount -= $amount / 100 * $user->agn_discountrate;
				    }    				        
				}else
				{
					$amount = $price - $price / 100 * $user->agn_discountrate;
				}
				return (round ($amount,2));
			 }else 
			 {			     
			    //if set "Apply only for selected days of week" but our current date not in this list so return original price 
			    $current_day_of_week = strtolower(date("D", strtotime($date))); 
			 	if ($sd->usedow == 1 && $sd->{"use_".$current_day_of_week} != 1)     
			 	{
						$amount = $price;
						return (round ($amount,2));
				}

				$amount = $price; 
				
				if($isaff && $affID>0) //if affiliate
				{
				    if($sd->inclaff == 1)
				    {
                        if($sd->affiliate > 0)
                        {                            
                            if($sd->affiliate==$affID) //only for selected affiliate
                            {
                               $amount = $sd->calculate($price);     
                            }                            
                        }else
                        {
                            $amount = $sd->calculate($price);
                        }
				    }
				}else
				{
				   if($sd->affiliate == 0)
                   {		    
				       $amount = $sd->calculate($price);
                   }                          
				}				
			 }
			 return (round ($amount,2));
		} 
        //now try to check specials for selected item
		$sd = new SiteDiscountBO();
		$dsk = new SiteDiscountItemBO ();
		$sd->leftJoin ($dsk);
		$dsk->item = $item;
		$sd->Load(0,0,"",$where);
		if ($sd->next()) 
		{
			if ($is_agent) //if agent
			{
				if ($sd->inclagents == 1) 
				{
					if ($sd->usedow == 1) 
					{
						$current_day_of_week = strtolower(date("D", strtotime($date)));
						if ($sd->{"use_".$current_day_of_week} != 1) //if selected day not TODAY...return agent's discount 
						{
							$amount = $price - $price / 100 * $user->agn_discountrate;
							return (round ($amount,2));
						}
					}
					$amount  = $sd->calculate($price);
					$amount -= $amount / 100 * $user->agn_discountrate;
				} else 
				{
					$amount = $price - $price / 100 * $user->agn_discountrate;
				}
				return (round ($amount,2));
			}else 
			{
				if ($sd->usedow == 1) 
				{
					$current_day_of_week = strtolower(date("D", strtotime($date)));
					if ($sd->{"use_".$current_day_of_week} != 1) 
					{
						$amount = $price;
						return (round ($amount,2));
					}
				}
				
				$amount = $price; 
				
				if($isaff) //if affiliate
				{
				    if($sd->inclaff == 1)
				    {
                        if($sd->affiliate > 0)
                        {                            
                            if($sd->affiliate==$affID) //only for selected affiliate
                            {
                               $amount = $sd->calculate($price);     
                            }                            
                        }else
                        {
                            $amount = $sd->calculate($price);
                        }
				    }
				}else
				{
				   if($sd->affiliate == 0)
                   {		    
				       $amount = $sd->calculate($price);
                   }                          
				}				
				return (round ($amount,2));
			}
		} else 
  		{ //No discounts
	        if ($is_agent) 
	        {
	          $amount = $price - $price / 100 * $user->agn_discountrate; 
	          return (round ($amount,2));
	        }else return (round ($price,2));
		}			  
	}

	/*
	 * return discount text
	 */
	function returnDiscount ($item, $date, $isaff, $userid, $affID=0) 
	{
	    if(isset($_SESSION["AGENT_CODE"]) && $_SESSION["AGENT_CODE"]=="gc") //no discounts for GC
	    {
		   return "";  
  	    }
	    
    	$is_agent = false;
      	if($userid!="")
      	{
      		$user = new UserBO ();
      		$user->id = $userid;
      		$user->Load();
      		if($user->Next()){
            	if($user->agn_agent=="1") $is_agent = true;
          	}
      	}
      	
      	$result = "";
	  	if($isaff && $affID>0) //if affiliate...need to check "ONLY FOR" this affiliate
	  	{
	  	        //now try to check specials for ALL items
        		$sd = new SiteDiscountBO();
        		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and ".$sd->fields["type"]."<3)";
        		$sd->allitems  = 1;
        		$sd->affiliate = $affID;
        		$sd->Load(0,1,"",$where);
        		if ($sd->Next()) //all items
        		{
    			    //if set "Apply only for selected days of week" but our current date not in this list so return original price 
    			    $current_day_of_week = strtolower(date("D", strtotime($date))); 
    			 	if ($sd->usedow == 1)     
    			 	{
    			 	    if($sd->{"use_".$current_day_of_week} == 1)
    			 	    {
            			    return $sd->asString();    			 	        
    			 	    }
    				}else
    				{       
    				    return $sd->asString(); 				                       
    				}    
        		}

        		//now try to check specials for selected item        		
    			$sd             = new SiteDiscountBO();
    			$sd->affiliate  = $affID;
    			$dsk            = new SiteDiscountItemBO ();
    			$sd->leftJoin ($dsk);
    			$dsk->item      = $item;        			
    			$sd->Load(0,1,"",$where);
    			if ($sd->next())
    			{
    			    $current_day_of_week = strtolower(date("D", strtotime($date))); 
    			 	if ($sd->usedow == 1)     
    			 	{
    			 	    if($sd->{"use_".$current_day_of_week} == 1)
    			 	    {
    			 	        return $sd->asString();
    			 	    }
    				}else
    				{    
    				    return $sd->asString();    				                       
    				}    
    			}        		
	  	}//end of ONLY FOR AFFILIATE      	
      	   	
		$sd = new SiteDiscountBO();
		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and ".$sd->fields["type"]."<3)";
		$sd->allitems = 1;
		$sd->Load(0,0,"",$where);
		if ($sd->next())  //all items 
    	{   
			if ($is_agent) 
			{
				if ($sd->inclagents == 1) 
				{
					if ($sd->usedow == 1) 
					{
						$current_day_of_week = strtolower(date("D", strtotime($date)));
						if ($sd->{"use_".$current_day_of_week} == 1) 
						{
							$result = $sd->asString();
						}
					}else
					{
					    $result = $sd->asString();    
					}					
				}else 
				{
					$result = "";
				}
				
				if ($result != "FREE") 
				{
					if ($result != "") 
					    $result = $result." + Agent Discount: ".$user->agn_discountrate."%";
					else
						$result = "Agent Discount: ".$user->agn_discountrate."%";
				}
				return $result;
			}else 
			{
				if($sd->usedow == 1) 
				{
					$current_day_of_week = strtolower(date("D", strtotime($date)));
					if ($sd->{"use_".$current_day_of_week} != 1) 
					{
						return "";
					}
			    }
			    
			    if(!$isaff || $sd->inclaff == 1) 
			    {
			        $result = $sd->asString();
				} else 
				{
					$result = "";
				}
				return $result;
			}
		}else //now need to check specials for selected item
        {
			$sd = new SiteDiscountBO();
			$dsk = new SiteDiscountItemBO ();
			$sd->leftJoin ($dsk);
			$dsk->item = $item;
			$sd->Load(0,0,"",$where);
			if ($sd->next()) 
            {
				if ($is_agent) 
				{
					if ($sd->inclagents == 1) 
					{
						if ($sd->usedow == 1) 
					    {
							$current_day_of_week = strtolower(date("D", strtotime($date)));
							if ($sd->{"use_".$current_day_of_week} == 1) 
							{
								$result = $sd->asString();
							}
						}else
						{
						    $result = $sd->asString();
						}
					}else 
					{
						$result = "";
					}
					
					if ($result != "FREE") 
					{
						if ($result != "")
							$result = $result." + Agent Discount: ".$user->agn_discountrate."%";
						else
							$result = "Agent Discount: ".$user->agn_discountrate."%";
					}
					return $result;
				}else 
				{
					if ($sd->usedow == 1) 
					{
						$current_day_of_week = strtolower(date("D", strtotime($date)));
						if ($sd->{"use_".$current_day_of_week} != 1) 
						{
							return "";
						}
					}
					
				    if ( !$isaff || $sd->inclaff == 1) 
				    {
				        $result = $sd->asString();
					}else 
					{
						$result = "";
					}
					return $result;
				}
			} else 
            { //No discounts
                if ($is_agent) 
                {
                  return "Agent Discount: ".$user->agn_discountrate."%";
                }else return "";
			}
		}
	}
	
	//is BOGOF - buy one get one free
	function isBOGOF ($item, $date, $isaff, $userid) 
    {  
    	if(isset($_SESSION["AGENT_CODE"]) && $_SESSION["AGENT_CODE"]=="gc")
    	{
    		   return false;  
    	}
        
	    $is_agent = false;
  	    if($userid!="")
  	    {
  		    $user = new UserBO ();
  		    $user->id = $userid;
  		    $user->Load();
  		    if($user->Next()){
        	        if($user->agn_agent=="1") $is_agent = true;
      	    }
  	    }
  	
		$sd = new SiteDiscountBO();
		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and ".$sd->fields["type"]." = 3)";
		$sd->allitems = 1;
		$sd->Load(0,0,"",$where);
		if ($sd->next()) 
        { //all items
			if ($is_agent) 
            {
				if ($sd->inclagents == 1) 
                {
     			    return true;
				}else 
                {
					return false;
				}				
			}else 
            {
			  if ( !$isaff || $sd->inclaff == 1) 
              {
                  return true;  
			  }else
              {
					return false;
			  }			
			}
		}else 
        {
			$sd  = new SiteDiscountBO();
			$dsk = new SiteDiscountItemBO ();
			$sd->leftJoin ($dsk);
			$dsk->item = $item;
			$sd->Load(0,0,"",$where);
			if ($sd->next()) 
            {
				if ($is_agent) 
                {
					if ($sd->inclagents == 1) 
                    {
                        return true;  
 				    }else
                    {
					  return false;
				    }			
				} else 
                {
				   if ( !$isaff || $sd->inclaff == 1) 
                   {
                       return true;  
  				   }else
                   {
  					   return false;
  				   }			
				}
			}else 
            { //No discounts
                  return false;
			}
		}
	}
	
	
	//is BOGOD - buy one get one with discount
	function isBOGOD ($item, $date, $isaff, $userid) 
    {  
        $result = array();
        $result["type"]   = "";
        $result["amount"] = "";         
    	if(isset($_SESSION["AGENT_CODE"]) && $_SESSION["AGENT_CODE"]=="gc")
    	{
    	       $result["exist"] = false; 
    		   return $result;  
    	}
        
	    $is_agent = false;
  	    if($userid!="")
  	    {
  		    $user = new UserBO ();
  		    $user->id = $userid;
  		    $user->Load();
  		    if($user->Next()){
        	        if($user->agn_agent=="1") $is_agent = true;
      	    }
  	    }
  	
		$sd = new SiteDiscountBO();
		$where = "('".$date."' < ".$sd->fields["finishdate"]." and '".$date."' >= ".$sd->fields["startdate"]." and (".$sd->fields["type"]." = 4 OR ".$sd->fields["type"]." = 5))";
		$sd->allitems = 1;
		$sd->Load(0,0,"",$where);
		if ($sd->next()) 
        { //all items
            $result["type"]   = $sd->type == 4 ? "%" : "$";
            $result["amount"] = $sd->amount;            
			if ($is_agent) 
            {
				if ($sd->inclagents == 1) 
                {
                    $result["exist"]  = true;
     			    return $result;
				}else 
                {
                    $result["exist"] = false;
					return $result;
				}				
			}else 
            {
			  if ( !$isaff || $sd->inclaff == 1) 
              {
                  $result["exist"] = true;
                  return $result;  
			  }else
              {
				  $result["exist"] = false;
				  return $result;                  
			  }			
			}
		}else 
        {
			$sd  = new SiteDiscountBO();
			$dsk = new SiteDiscountItemBO ();
			$sd->leftJoin ($dsk);
			$dsk->item = $item;
			$sd->Load(0,0,"",$where);
			if ($sd->next()) 
            {
                $result["type"]   = $sd->type == 4 ? "%" : "$";
                $result["amount"] = $sd->amount;            
                
				if ($is_agent) 
                {
					if ($sd->inclagents == 1) 
                    {
                        $result["exist"] = true;
                        return $result;                          
 				    }else
                    {
				        $result["exist"] = false;
				        return $result;                  
				    }			
				} else 
                {
				   if ( !$isaff || $sd->inclaff == 1) 
                   {
                       $result["exist"] = true;
                       return $result;                          
  				   }else
                   {
				       $result["exist"] = false;
				       return $result;                  
  				   }			
				}
			}else 
            { //No discounts
				  $result["exist"] = false;
				  return $result;                  
			}
		}
	}	
}
?>